﻿using System;

using Infrastructure.NetExtensions;
using TempEmployees.DAL;
using TempEmployees.BLL.EntitiesExtensions;

namespace TempEmployees.Web
{
    public partial class ProduceForms : System.Web.UI.Page
    {
        protected string SourcePage
        {
            get { return ViewState.Get<string>("source_page"); }
            set
            {
                ViewState.Set("source_page", value);
                LinkButtonBack.Visible = true;
            }
        }

        protected string EmployeeID
        {
            get { return ViewState.Get<string>("employee"); }
            set { ViewState.Set<string>("employee", value); }
        }

        protected Employee Employee { get { return EEmployee.GetEmployeeByID(EmployeeID); } }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (APageData.Exist<ProduceEmployeeFormsData>())
                {
                    ProduceEmployeeFormsData data = APageData.Get<ProduceEmployeeFormsData>();
                    EmployeeID = data.EmployeeID;
                    SourcePage = data.SourcePage;
                    data.Remove();
                    SetDisplay(Employee);
                }
            }
        }

        protected void LinkButtonBack_Click(object sender, EventArgs e)
        {
            Response.Redirect(SourcePage);
        }

        protected void EmployeeIDButton_Click(object sender, EventArgs e)
        {
            string employeeId = EmployeeIDTextBox.Text;
            if (string.IsNullOrEmpty(employeeId)) return;

            Employee employee = EEmployee.GetEmployeeByID(employeeId);

            if (employee != null) EmployeeID = employeeId;

            SetDisplay(employee);
        }

        protected void ProduceEmployeeDetails_Click(object sender, EventArgs e)
        {
            TempEmployees.BLL.Forms.ProduceForms.ProduceEmployeeDetails(Employee);
        }

        protected void ProduceMosadStudent_Click(object sender, EventArgs e)
        {
            TempEmployees.BLL.Forms.ProduceForms.ProduceMosadStudent(Employee.CurrentAssignment);
        }

        protected void ProduceNewEmployee_Click(object sender, EventArgs e)
        {
            TempEmployees.BLL.Forms.ProduceForms.ProducePrintNewEmployee(Employee.CurrentAssignment);
        }

        protected void SetDisplay(Employee employee)
        {
            if (employee == null || employee.CurrentAssignment == null)
            {
                ProduceButtonsSection.Visible = false;
            }
            else
            {
                ProduceButtonsSection.Visible = true;
                EmployeeIDLabel.Text = employee.ID;
            }
        }
    }

    public class ProduceEmployeeFormsData : APageData
    {
        public string EmployeeID { get; private set; }
        public string SourcePage { get; private set; }

        public ProduceEmployeeFormsData(string employeeId, string sourcePage)
        {
            EmployeeID = employeeId;
            SourcePage = sourcePage;
        }
    }
}